
import com.cysyz.mylib.domain.annotation.Alias;

<#assign className = table.className>
<#assign classNameLower = className?uncap_first> 
package ${basepackage}.domain;

import com.cysyz.mylib.domain.BaseEntity;
import com.cysyz.springmvc.support.permission.domain.SysUserRole;
import com.cysyz.org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import com.cysyz.org.hibernate.validator.constraints.NotBlank;
import com.cysyz.mylib.utils.DateUtil;
import javax.persistence.*;

@Entity
@Table(name = "${table.sqlName}")
public class ${className} extends BaseEntity {
	private static final long serialVersionUID = 5454155825314635342L;
	
	//alias
	public static final String TABLE_ALIAS = "${table.tableAlias}";
	<#list table.columns as column>
<#if !['id', 'version']?seq_contains(column.sqlName)>
	public static final String ALIAS_${column.constantName} = "${column.columnAlias}";
</#if>
	</#list>
	
	//date formats
	<#list table.columns as column>
	<#if column.isDateTimeColumn>

	public static final String FORMAT_${column.constantName} = DATE_FORMAT;
	</#if>
	</#list>
	
	<@generateFields/>
	@Override
	public String[] businessKeys() {
		throw new RuntimeException("请配置业务主键");
	}
	<@generateNotPkProperties/>
	<@generateJavaOneToMany/>
	<@generateJavaManyToOne/>
	/*vo*/
	<@generateSearchCondsVoInfo/>

	public String toString() {
		return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
		<#list table.columns as column>
<#if !['id', 'version']?seq_contains(column.sqlName)>
			<#if !table.compositeId>
			.append("${column.columnName}",get${column.columnName}())
			</#if>
</#if>
		</#list>
			.toString();
	}
}

<#macro generateFields>

	//可以直接使用: @Length(max=50,message="用户名长度不能大于50")显示错误消息
	//columns START
	<#list table.columns as column>
		<#if !['id', 'version']?seq_contains(column.sqlName)>
	${column.hibernateValidatorExprssion}
	@Column(name = "${column.sqlName}", unique = ${column.unique?string}, nullable = ${column.nullable?string}, length = ${column.size})
	@Alias(cn="${column.remarks?trim}")
	private ${column.javaType} ${column.columnNameLower};

		</#if>
	</#list>
	//columns END

</#macro>

<#--
	生成在条件搜索中使用到的属性，一般情况下，在条件搜索中有一些实体类中的一个属性， 需要一个范围去限定它。
	如属性是日期类型，那么就生成该属性名称+Begin/End的两个属性，
	如果是其他的范围类型（一般情况下是数字）就生成该属性名称+Min/Max的两个属性，
-->
<#macro generateSearchCondsVoInfo>
<#list table.columns as column>

<#if column.isRangeType && (column.extColumnMetaData.searchCond)!false>
	<#if column.isDateTimeColumn>
	@Transient
	private ${column.javaType} ${column.columnNameLower}Begin;
	@Transient
	private ${column.javaType} ${column.columnNameLower}End;
	<#else>
	@Transient
	private ${column.javaType} ${column.columnNameLower}Min;
	@Transient
	private ${column.javaType} ${column.columnNameLower}Max;
	</#if>
</#if>
</#list>

<#list table.columns as column>
<#if column.isRangeType && (column.extColumnMetaData.searchCond)!false>
<#if column.isDateTimeColumn>
	@Transient
	public ${column.javaType} get${column.columnName}Begin() {
		return this.${column.columnNameLower}Begin;
	}

	public void set${column.columnName}Begin(${column.javaType} value) {
		this.${column.columnNameLower}Begin = value;
	}

	@Transient
	public ${column.javaType} get${column.columnName}End() {
		return this.${column.columnNameLower}End;
	}

	public void set${column.columnName}End(${column.javaType} value) {
		this.${column.columnNameLower}End = value;
	}
<#else>
	public ${column.javaType} get${column.columnName}Min() {
		return this.${column.columnNameLower}Min;
	}

	public void set${column.columnName}Min(${column.javaType} value) {
		this.${column.columnNameLower}Min = value;
	}

	@Transient
	public ${column.javaType} get${column.columnName}Max() {
		return this.${column.columnNameLower}Max;
	}

	public void set${column.columnName}Max(${column.javaType} value) {
		this.${column.columnNameLower}Max = value;
	}

</#if>
</#if>
</#list>


</#macro>


<#macro generateCompositeIdConstructorIfis>

	<#if table.compositeId>
	public ${className}(){
	}
	public ${className}(${className}Id id) {
		this.id = id;
	}
	<#else>
	<@generateConstructor className/>
	</#if>
	
</#macro>

<#macro generateNotPkProperties>
	<#list table.columns as column>
<#if !['id', 'version']?seq_contains(column.sqlName)>
		<#if !column.pk>
			<#if column.isDateTimeColumn>
	@Transient
	public String get${column.columnName}String() {
		return DateUtil.format(get${column.columnName}(), FORMAT_${column.constantName});
	}
	
	</#if>
	public ${column.javaType} get${column.columnName}() {
		return this.${column.columnNameLower};
	}
	
	public void set${column.columnName}(${column.javaType} value) {
		this.${column.columnNameLower} = value;
	}
	
		</#if>
</#if>
	</#list>
</#macro>

<#macro generateJavaOneToMany>
	<#list table.exportedKeys.associatedTables?values as foreignKey>
	<#assign fkSqlTable = foreignKey.sqlTable>
	<#assign fkTable    = fkSqlTable.className>
	<#assign fkPojoClass = fkSqlTable.className>
	<#assign fkPojoClassVar = fkPojoClass?uncap_first>
	
	private Set ${fkPojoClassVar}s = new HashSet(0);
	public void set${fkPojoClass}s(Set<${fkPojoClass}> ${fkPojoClassVar}){
		this.${fkPojoClassVar}s = ${fkPojoClassVar};
	}
	
	@OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "${classNameLower}")
	public Set<${fkPojoClass}> get${fkPojoClass}s() {
		return ${fkPojoClassVar}s;
	}
	</#list>
</#macro>

<#macro generateJavaManyToOne>
	<#list table.importedKeys.associatedTables?values as foreignKey>
	<#assign fkSqlTable = foreignKey.sqlTable>
	<#assign fkTable    = fkSqlTable.className>
	<#assign fkPojoClass = fkSqlTable.className>
	<#assign fkPojoClassVar = fkPojoClass?uncap_first>
	
	private ${fkPojoClass} ${fkPojoClassVar};
	
	public void set${fkPojoClass}(${fkPojoClass} ${fkPojoClassVar}){
		this.${fkPojoClassVar} = ${fkPojoClassVar};
	}
	
	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	<#list foreignKey.parentColumns?values as fkColumn>
	@JoinColumn(name = "${fkColumn}",nullable = false, insertable = false, updatable = false)
    </#list>
	public ${fkPojoClass} get${fkPojoClass}() {
		return ${fkPojoClassVar};
	}
	</#list>
</#macro>

